package com.jboltai.util.text;

import com.jboltai.config.JBoltAIConfig;
import com.jboltai.event.text.IFile2TextConverter;
import com.jboltai.event.text.IFileStreamExtractor;
import com.jboltai.event.text.TextEvent;
import org.apache.tika.extractor.EmbeddedDocumentExtractor;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.sax.ContentHandlerDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.function.Consumer;

public abstract class BaseContentHandler extends ContentHandlerDecorator implements EmbeddedDocumentExtractor {
    protected static final Logger LOGGER = LoggerFactory.getLogger(JBoltAIConfig.LOGGER_NAME);
    protected static final String TAG_P = "p";
    protected static final String TAG_IMG = "img";
    protected static final String TAG_DIV = "div";
    protected static final String SEPARATOR = "\n";

    protected IFile2TextConverter file2TextConverter;

    protected IFileStreamExtractor fileStreamExtractor;

    protected Consumer<String> consumer;

    protected Map<String, FileInfo> fileInfoMap;

    protected TextEvent event;

    protected boolean hasContent = false;

    protected String temp;

    public BaseContentHandler(Consumer<String> consumer) {
        if (consumer == null) {
            throw new IllegalArgumentException("consumer can not be null");
        }
        this.consumer = consumer;
    }

    public BaseContentHandler(Consumer<String> consumer, TextEvent event, Map<String, FileInfo> fileInfoMap) {
        if (consumer == null) {
            throw new IllegalArgumentException("consumer can not be null");
        }
        this.consumer = consumer;
        this.event = event;
        this.fileInfoMap = fileInfoMap;
        if (event != null) {
            this.file2TextConverter = event.getFile2TextConverter();
//            this.fileStreamExtractor = event.getFileStreamExtractor();
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) {
        temp = new String(ch, start, length);
        consumer.accept(temp);
        if (length > 0 && !temp.equals(SEPARATOR)) {
            hasContent = true;
        }
    }

    public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
        super.startElement(uri, localName, name, attrs);
    }

    public void endElement(String uri, String localName, String name) throws SAXException {
        super.endElement(uri, localName, name);
        if (TAG_P.equals(localName) && hasContent) {
            consumer.accept(SEPARATOR);
        }
    }

    @Override
    public boolean shouldParseEmbedded(Metadata metadata) {
        return true;
    }

    @Override
    public abstract void parseEmbedded(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, boolean b) throws SAXException, IOException;
}
